package models

import (
	"math/rand"
	"myRbac/utils"
)

// 用户表结构体
type User struct {
	// 用户Id，唯一标识
	Id int `json:"id" form:"id" gorm:"column:id;type:int;auto_increment;primary_key;"`
	// 父Id，创建者Id
	Fid int `json:"fid" form:"fid" gorm:"column:fid;type:int;"`
	// 用户描述
	Description string `json:"description" form:"description" gorm:"column:description;type:varchar(120);"`
	// 英文名
	Englishname string `json:"englishname" form:"englishname" gorm:"column:englishname;type:varchar(40);"`
	// 用户密码，sha256加密
	Password string `json:"password" form:"password" gorm:"column:password;type:varchar(120);"`
	// 联系电话
	Phone string `json:"phone" form:"phone" gorm:"column:phone;type:varchar(18);"`
	// 邮箱
	Email string `json:"email" form:"email" gorm:"column:email;type:varchar(28);"`
	// 用户状态
	Status int `json:"status" form:"status" gorm:"column:status;type:int;"`
	// 账户名
	Account string `json:"account" form:"account" gorm:"column:account;type:varchar(40);uniqueindex;"`
	// 用户名
	Username string `json:"username" form:"username" gorm:"column:username;type:varchar(40);"`
	// 性别
	Gender int `json:"gender" form:"gender" gorm:"column:gender;type:int;"`
	// 删除标记
	IsDelete int `json:"is_delete" form:"is_delete" gorm:"column:is_delete;type:int;"`
	// 创建角色关联表
	Role []Role `json:"roles" form:"-" gorm:"many2many:user_role;"`
	// 所属部门
	Department int `json:"department" form:"department"`
}

// 简化查询结构体
type UserSelect struct {
	Id          int    `json:"id"`
	Description string `json:"description"`
	Englishname string `json:"englishname"`
	Phone       string `json:"phone"`
	Username    string `json:"username"`
	Gender      int    `json:"gender"`
}

func (u *User) GetUserSelect() map[string]interface{} {
	return map[string]interface{}{
		"Id":          u.Id,
		"Description": u.Description,
		"Englishname": u.Englishname,
		"Phone":       u.Phone,
		"Username":    u.Username,
		"Gender":      u.Gender,
	}
}

// 插入一批用户数据
func InsertMockUser() {
	DB.Model(&User{}).Create([]User{
		{Fid: 0, Description: "无关风月", Englishname: utils.GetEnglishName(), Password: utils.Sha256("123456"), Email: "31213@qq.com", Phone: utils.GetMockPhone(), Account: "admin", Username: utils.GetFullName(), Gender: rand.Intn(2) + 1, Department: 1, IsDelete: 0, Status: 1},
		{Fid: 1, Description: "无关风月", Englishname: utils.GetEnglishName(), Password: utils.Sha256("123456"), Email: "31213@qq.com", Phone: utils.GetMockPhone(), Account: "user1", Username: utils.GetFullName(), Gender: rand.Intn(2) + 1, Department: 1, IsDelete: 0, Status: 1},
		{Fid: 1, Description: "我题序等你回", Englishname: utils.GetEnglishName(), Password: utils.Sha256("123456"), Email: "31213@qq.com", Phone: utils.GetMockPhone(), Account: "user2", Username: utils.GetFullName(), Gender: rand.Intn(2) + 1, Department: 1, IsDelete: 0, Status: 1},
		{Fid: 2, Description: "悬笔一绝", Englishname: utils.GetEnglishName(), Password: utils.Sha256("123456"), Email: "31213@qq.com", Phone: utils.GetMockPhone(), Account: "user3", Username: utils.GetFullName(), Gender: rand.Intn(2) + 1, Department: 1, IsDelete: 0, Status: 1},
		{Fid: 2, Description: "那岸上浪千叠", Englishname: utils.GetEnglishName(), Password: utils.Sha256("123456"), Email: "31213@qq.com", Phone: utils.GetMockPhone(), Account: "user4", Username: utils.GetFullName(), Gender: rand.Intn(2) + 1, Department: 2, IsDelete: 0, Status: 1},
		{Fid: 2, Description: "情字何解", Englishname: utils.GetEnglishName(), Password: utils.Sha256("123456"), Email: "31213@qq.com", Phone: utils.GetMockPhone(), Account: "user5", Username: utils.GetFullName(), Gender: rand.Intn(2) + 1, Department: 5, IsDelete: 0, Status: 1},
		{Fid: 3, Description: "怎落笔都不对", Englishname: utils.GetEnglishName(), Password: utils.Sha256("123456"), Email: "31213@qq.com", Phone: utils.GetMockPhone(), Account: "user6", Username: utils.GetFullName(), Gender: rand.Intn(2) + 1, Department: 4, IsDelete: 0, Status: 1},
		{Fid: 3, Description: "而我独缺", Englishname: utils.GetEnglishName(), Password: utils.Sha256("123456"), Email: "31213@qq.com", Phone: utils.GetMockPhone(), Account: "user7", Username: utils.GetFullName(), Gender: rand.Intn(2) + 1, Department: 1, IsDelete: 0, Status: 1},
		{Fid: 4, Description: "你一生的了解", Englishname: utils.GetEnglishName(), Password: utils.Sha256("123456"), Email: "31213@qq.com", Phone: utils.GetMockPhone(), Account: "user8", Username: utils.GetFullName(), Gender: rand.Intn(2) + 1, Department: 3, IsDelete: 0, Status: 1},
		{Fid: 4, Description: "手书无愧", Englishname: utils.GetEnglishName(), Password: utils.Sha256("123456"), Email: "31213@qq.com", Phone: utils.GetMockPhone(), Account: "user9", Username: utils.GetFullName(), Gender: rand.Intn(2) + 1, Department: 2, IsDelete: 0, Status: 1},
		{Fid: 5, Description: "无惧人间是非", Englishname: utils.GetEnglishName(), Password: utils.Sha256("123456"), Email: "31213@qq.com", Phone: utils.GetMockPhone(), Account: "user10", Username: utils.GetFullName(), Gender: rand.Intn(2) + 1, Department: 1, IsDelete: 0, Status: 1},
		{Fid: 10, Description: "雨打蕉叶", Englishname: utils.GetEnglishName(), Password: utils.Sha256("123456"), Email: "31213@qq.com", Phone: utils.GetMockPhone(), Account: "user11", Username: utils.GetFullName(), Gender: rand.Intn(2) + 1, Department: 2, IsDelete: 0, Status: 1},
		{Fid: 10, Description: "又潇潇了几夜", Englishname: utils.GetEnglishName(), Password: utils.Sha256("123456"), Email: "31213@qq.com", Phone: utils.GetMockPhone(), Account: "user12", Username: utils.GetFullName(), Gender: rand.Intn(2) + 1, Department: 6, IsDelete: 0, Status: 1},
	})
}
